import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { apiConf } from '@conf/api.conf';
import { STColumn, STComponent } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { ModalHelper, _HttpClient, DatePipe } from '@delon/theme';
import { NzImageService } from 'ng-zorro-antd/image';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ImageViewComponent } from 'src/app/shared/components/imagelist';
import { UsermanageService } from '../../../../services/usercenter.service';

@Component({
  selector: 'app-supplier-components-list-view',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.less'],
  providers:[DatePipe]
})
export class FreightComponentsUserDetailComponent implements OnInit {
  @ViewChild('redectModal', { static: false })
  redectModal!: any;

  userDetail: any = {};
  userIdentityDetail: any = {};
  tempalateUserIdentityDetail = { ...this.userIdentityDetail };
  @ViewChild('st', { static: false }) st!: STComponent;
  columns: STColumn[] = [
    { title: '企业名称', className: 'text-center', index: 'enterpriseName' },
    { title: '项目名称', className: 'text-center', index: 'projectName' },
    { title: '角色', className: 'text-center', index: 'roleName' }
  ];

  approvalOpinion = '';
  reasonTags = ['身份证照片太丑', '姓名与身份证号不匹配'];

  isEditUser = false;
  uploadURl = apiConf.waterFileUpload;
  disabledUpload = false;
  constructor(
    private nzModalService: NzModalService,
    public service: UsermanageService,
    public route: ActivatedRoute,
    private nzImageService: NzImageService,
    private datePipe: DatePipe
  ) {}

  ngOnInit() {
    this.initData();
    // this.launchSign();
  }
  async initData() {
    // 获取司机头部信息
    this.service
      .request(this.service.$api_get_user_detail, {
        appUserId: this.route.snapshot.params.id
      })
      .subscribe(res => {
        if (res) {
          this.userDetail = res;
        }
      });
    // 获取用户个人信息
    this.service
      .request(this.service.$api_get_user_identity, {
        id: this.route.snapshot.params.id
      })
      .subscribe(res => {
        if (res) {
          this.userIdentityDetail = res;
          this.tempalateUserIdentityDetail = { ...this.userIdentityDetail };
        }
      });
  }

  userAction(status: number) {
    this.nzModalService.warning({
      nzTitle: status === 1 ? '确定启用该用户吗？' : '确定冻结该用户吗？',
      nzContent:
        status === 1
          ? '启用后，该用户将恢复正常使用功能，请再次确认'
          : '停用后，该用户将被限制使用，不限于访问受限、无法发布货源等，请谨慎操作',
      nzOnOk: () => {
        this.service
          .request(this.service.$api_lock_or_free_user, {
            appUserId: [this.userDetail.appUserId],
            freezeOrResume: !!!status,
            pageName: '货主员工',
            telephone: this.userDetail.phone
          })
          .subscribe(res => {
            if (res) {
              this.service.msgSrv.success('操作成功');
            }
            this.initData();
          });
      }
    });
  }

  auditPass() {
    this.nzModalService.confirm({
      nzTitle: '审核通过',
      nzContent: `是否确认通过(姓名:${this.userIdentityDetail?.name})审核`,
      nzOnOk: () => {
        this.auditEnterprise(0);
      }
    });
  }

  auditNo() {
    this.approvalOpinion = '';
    this.nzModalService.create({
      nzTitle: '审核驳回',
      nzContent: this.redectModal,
      nzOnOk: () => {
        if (!this.approvalOpinion) {
          return false;
        }
        this.auditEnterprise(1);
        return;
      }
    });
  }

  private auditEnterprise(auditStatus: number) {
    this.service
      .request(this.service.$api_approve_identity, {
        auditStatus: auditStatus,
        identityId: this.userIdentityDetail.id,
        auditType: 0,
        certificationOpinions: this.approvalOpinion
      })
      .subscribe(res => {
        if (res) {
          this.service.msgSrv.success(auditStatus === 0 ? '审核通过' : '驳回成功');
        }
        this.initData();
      });
  }

  /**
   * 开启修改
   * @param type 修改类型
   */
  ratify() {
    this.isEditUser = true;
  }

  /**
   * 需求修改
   * @param type 修改类型
   */
  reset() {
    this.userIdentityDetail = { ...this.tempalateUserIdentityDetail };
    this.isEditUser = false;
  }

  saveUser() {
    const params = {...this.userIdentityDetail};
    Object.assign(params, {
      ...this.userIdentityDetail,
      validStartTime: this.datePipe.transform(this.userIdentityDetail?.validStartTime, 'yyyy-MM-dd'),
      validEndTime: this.datePipe.transform(this.userIdentityDetail?.validEndTime, 'yyyy-MM-dd'),
    });
    this.service.request(this.service.$api_update_driver_identity, params).subscribe(res => {
      if (res) {
        this.service.msgSrv.success('修改成功');
        this.isEditUser = false;
        this.initData();
      }
    });
  }

  changeUpload({ file, fileList, type }: any, data: any, key: string, key2: string) {
    if (type === 'success') {
      data[key] = file.response.data?.fullFileWatermarkPath;
      data[key2] = file.response.data?.fullFilePath;
    }
  }

  showImg(url: any) {
    const params = {
      imgList: [url],
      index: 0
    };
    this.nzImageService.preview([{ src: url }]);
    // this.nzModalService.create({ nzContent: ImageViewComponent, nzComponentParams: { params } });
  }

  deleteImg(data: any, key: string, key2: string) {
    this.nzModalService.warning({
      nzTitle: '是否确认删除该图片',
      nzOnOk: () => {
        this.disabledUpload = true;
        data[key] = '';
        data[key2] = '';
        setTimeout(() => {
          this.disabledUpload = false;
        }, 100);
      }
    });
  }

  goBack() {
    window.history.go(-1);
  }
}
